﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <!--<script src="../js/jquery/2/jquery-2.1.1.js"></script>-->
    <script src="js/jquery/jquery-1.8.1.js"></script>
    <!--<script src="../js/jquery/jquery-1.8.1.min.js"></script>-->
    <script src="js/other/JSON.js"></script>

    <script src="js/bingo/console.js"></script>
    <script src="js/bingo/core.js"></script>
    <script src="js/bingo/datavalue.js"></script>
    <script src="js/bingo/Event.js"></script>
    <script src="js/bingo/variable.js"></script>
    <script src="js/bingo/Class.js"></script>
    <script src="js/bingo/linq.js"></script>
    <script src="js/bingo/equals.js"></script>
    <script src="js/bingo/fetch.js"></script>
    <script src="js/bingo/package.js"></script>
    <script src="js/bingo/route.js"></script>
    <script src="js/bingo/cache.js"></script>

    <script src="js/bingo/mv/linkToDom.js"></script>
    <script src="js/bingo/mv/module.js"></script>
    <script src="js/bingo/mv/factory.js"></script>
    <script src="js/bingo/mv/model.js"></script>
    <script src="js/bingo/mv/observer.js"></script>
    <script src="js/bingo/mv/ajax.js"></script>
    <script src="js/bingo/mv/compiles.js"></script>
    <script src="js/bingo/mv/view.js"></script>
    <script src="js/bingo/mv/filter.js"></script>
    <script src="js/bingo/mv/render.js"></script>

    <script src="js/bingo/mv/cmp/base.js"></script>

    <script src="js/bingo/mv/factory/base.js"></script>
    <script src="js/bingo/mv/factory/linq.js"></script>
    <script src="js/bingo/mv/factory/location.js"></script>
    <script src="js/bingo/mv/factory/render.js"></script>
    <script src="js/bingo/mv/factory/timeout.js"></script>

    <script src="js/bingo/mv/command/action.js"></script>
    <script src="js/bingo/mv/command/attrs.js"></script>
    <script src="js/bingo/mv/command/event.js"></script>
    <script src="js/bingo/mv/command/for.js"></script>
    <script src="js/bingo/mv/command/route.js"></script>
    <script src="js/bingo/mv/command/html.js"></script>
    <script src="js/bingo/mv/command/if.js"></script>
    <script src="js/bingo/mv/command/include.js"></script>
    <script src="js/bingo/mv/command/model.js"></script>
    <script src="js/bingo/mv/command/styles.js"></script>
    <script src="js/bingo/mv/command/text.js"></script>
    <script src="js/bingo/mv/command/node.js"></script>

    <script src="js/bingo/mv/filter/base.js"></script>
    
    
    <script src="test/lib.js"></script>
    <script src="test/route.js"></script>
    <script type="text/javascript">
        (function () {
            bingo.location.onLoadBefore(function(url, loc){console.log(url, loc);});
            return;
            var _tagStart = /\{\%/g,//开始标记
                _tagEnd = /\%\}/g;//结束标记


            //测试是否存在{%master file="" %}
            var _testMasterTagRegx = new RegExp(_tagStart.source + /\s*master\s+file=["']([^"']*?)["'].*?/.source + _tagEnd.source, 'gi');
            console.log('master true==', _testMasterTagRegx.test('{%master file="" %}')); //true
            console.log('master1 false==', _testMasterTagRegx.test('{%master1 file="" %}')); //false
            console.log('master file1 false==', _testMasterTagRegx.test('{%master file1="" %}')); //false

            //测试是否存在页面标签
            var _testPageTagRegx = new RegExp(_tagStart.source + /\s*(?:master|include|using|contentholder|content).*?/.source + _tagEnd.source, 'i');

            console.log('master true==', _testPageTagRegx.test('{%master file="" %}')); //true
            console.log('include true==', _testPageTagRegx.test('{%include file="" %}')); //true
            console.log('using true==', _testPageTagRegx.test('{%using file="" %}')); //true
            console.log('contentholder true==', _testPageTagRegx.test('{%contentholder id="" %}')); //true
            console.log('/contentholder false==', _testPageTagRegx.test('{%/contentholder%}')); //false
            console.log('content true==', _testPageTagRegx.test('{%content id="" %}')); //true
            console.log('/content false==', _testPageTagRegx.test('{%/content%}')); //false

            //匹配{%master file="aaa/master" %}类型
            var _fileTagRegx = new RegExp(_tagStart.source + /\s*(master|include|using)\s+file=["']([^"']+?)["']\s*/.source + _tagEnd.source, 'gi');
            //匹配{%content id="body"%}.....{%/content%}类型
            var _idTagRegx = new RegExp(_tagStart.source + /\s*(contentholder|content)\s+id=["']([^"']+?)["']\s*/.source + _tagEnd.source + /((?:.|\n|\r)*?)/.source + _tagStart.source + /\/\s*\1\s*/.source + _tagEnd.source, 'gi');
            $(function () {
                var html = document.body.innerHTML;
                _fileTagRegx.lastIndex = 0;
                var list = [], item;
                while (item = _fileTagRegx.exec(html)) {
                    list.push({
                        type: 'file',
                        find: item[0],
                        tag: item[1],//master|include|using
                        file: item[2]//file的值：view/user/list
                    });
                }
                console.table ? console.table(list) : console.log(list);


                _idTagRegx.lastIndex = 0;
                list = [], item;
                while (item = _idTagRegx.exec(html)) {
                    list.push({
                        type: 'id',
                        find: item[0],
                        tag: item[1],//contentholder|content
                        id: item[2],//id的值
                        content: item[3]//标签内容
                    });
                }
                console.table ? console.table(list) : console.log(list);
            });
        })();
        (function(){
return;
                var _cmps = {};
                bingo.cmp = function(p){
                    if(bingo.isObject(p)){
                        p  = bingo.extend({
                            //优先级, 越大越前, 默认100
                            priority: 100,
                            //是否加载模板
                            tmpl:false,
                            //是否引用js
                            using:false
                        }, p);
                        _cmps[p.name] = p;
                    } else
                        return _cmps[p];
                };

                bingo.cmp({
                    name:'master',
                    priority:900,
                    tmpl:true,
                    fn:function(p){
                        p.loadTemp(p.node.attr.file);
                    }
                });

                bingo.cmp({
                    name:'include',
                    tmpl:true,
                    fn:function(p){
                        p.loadTemp(p.node.attr.file);
                    }
                });

                bingo.cmp({
                    name:'using',
                    using:true,
                    fn:function(p){
                        p.using(p.node.attr.file);
                    }
                });

                bingo.cmp({
                    name:'contentholder',
                    priority:90,
                    fn:function(p){
                        var node = p.node, id = node.attr.id;
                        var contentNode = bingo.linq(p.allNodes).where(function(){
                            return this.name == 'content' && this.attr.id == id;
                        }).first();
                        p.replace(contentNode ? contentNode.content : '');
                    }
                });

                bingo.cmp({
                    name:'content',
                    priority:85,
                    fn:function(p){
                        p.replace('');
                    }
                });

        })();
        (function(){
            return;
            var _testCmpRegx = /<\s*cmp\:(\S+)\s+(?:.|\n|\r)*?(?:\/>|>)/i;
            console.log('master true==', _testCmpRegx.test('<cmp:master file="viewP/path/master" />')); //true
            console.log('master n true==', _testCmpRegx.test('<cmp:master \n\r file="viewP/path/master" />')); //true
            console.log('content true==', _testCmpRegx.test('<cmp:content id="header">')); //true

            $(function(){
                var html = '<cmp:master file="viewP/path/master" aaa=\'aa\' />' +document.body.outerHTML;
                var _cmpTagRegx = /<\s*cmp\:(\S+)\s+((?:.|\n|\r)+?)(?:\/>|>((?:.|\n|\r)*?)<\s*\/cmp:\1\s*>)/gi;
                var _cmpAttrRegx = /\s*(\S+)\s*=\s*(?:["]([^"]*?)["]|[']([^']*?)['])\s*/gi;


                var _cmpObj = {
                    loadTmpl:function(f){
                        var $this = this;
                        bingo.loadTmpl(f, function(h){
                            $this.replace(h);
                        })
                    },
                    using:function(f){
                        bingo.using(f);
                        this.replace('');
                    },
                    replace:function(s, s1){
                        if (arguments.length == 1)
                            this.html =  this.html.replace(this.find, s);
                        else
                            this.html =  this.html.replace(s, s1);
                    }
                };

                var _traverseIdTag = function(html){
                    var item, list = [], name, cmp;
                    _cmpTagRegx.lastIndex = 0;
                    while(item = _cmpTagRegx.exec(html)){
                        //console.log(item);
                        name = item[1];
                        cmp = bingo.cmp(name);
                        if (cmp)
                            list.push(bingo.extend({
                                find:item[0],
                                name:name,
                                node:{content:item[3], attr:traAttr(item[2])},
                                cmp:cmp
                            }, _cmpObj));
                    }
                    bingo.each(list, function(){
                        var c = this.content;
                        if (_testCmpRegx.test(c)){
                           list =list.concat(_traverseIdTag(c));
                        }
                    });
                    return list;
                }, traAttr = function(s){
                    var item, attr = {};
                    _cmpAttrRegx.lastIndex = 0;
                    while(item = _cmpAttrRegx.exec(s)){
                        attr[item[1]] = item[2] || item[3] || '';
                    }
                    return attr;
                };
                var list = _traverseIdTag(html);
                list = bingo.linq(list).sortDesc(function (item) { return item.cmp.priority; }).toArray();
                var hasLoad = bingo.linq(list).where(function (item) { return item.cmp.tmpl; }).contain();

                console.log(hasLoad, list);

            });


        })();
    </script>
</head>
<body>
    <cmp:master file="viewP/path/master" test="adffdaf
    dddd" test1="adsfdf"></cmp:master>
    <cmp:include file="viewP/path/master"></cmp:include>
    <cmp:using file="viewP/path/master"></cmp:using>

    <cmp:contentholder id="header">
    contentholder. text
    <cmp:using file="viewP/path/master"></cmp:using>
    aaa
    </cmp:contentholder>

    <cmp:content id="header">
    content. text
    <div>你好吗？</div>
    </cmp:content>
    <div bg-route="viewP/path/page1" bg-name="page1"></div>
</body>
</html>
